#ifndef _DEBUG_H_
#define _DEBUG_H_

#define CONFIG_ENABLE_DEBUG

typedef unsigned long   ULONG;
typedef uint8_t bool;

 /* Debug Level
 * DEBUG_LEVEL_DISABLE： No log is allowed output
 * DEBUG_LEVEL_ERR：     Only DBG_ERR is allowed output
 * DEBUG_LEVEL_INFO：    DBG_ERR and DBG_INFO are allowed output
 * DEBUG_LEVEL_DEBUG：   All log are allowed output
 */
typedef enum _DEBUG_LEVEL {
    DBG_LEVEL_DISABLE = 0,
    DBG_LEVEL_ERR,
    DBG_LEVEL_INFO,
    DBG_LEVEL_PROG,

    DBG_LEVEL_INVALID = 0xFF
} DEBUG_LEVEL;

DEBUG_LEVEL get_dbg_level ();
void set_dbg_level (DEBUG_LEVEL dbg_level);

#define FALSE                0                     
#define TRUE                 1                     

#define PRINT                 printf

#define DBG(level, ...)                                 \
do {                                                    \
    if (get_dbg_level () >= level) {                    \
        PRINT(__VA_ARGS__);                             \
        fflush(stdout);                             \
    }                                                   \
} while (0)

#define INFO(...)                                       \
do {                                                    \
    if (get_dbg_level () >= DBG_LEVEL_INFO) {           \
        PRINT(__VA_ARGS__);                             \
        fflush(stdout);                             \
    }                                                   \
} while (0)

#define DBG_INFO(fmt, ...)                                 \
do {                                                       \
    if (get_dbg_level () >= DBG_LEVEL_INFO) {              \
        PRINT(fmt"\n", ##__VA_ARGS__);    \
        fflush(stdout);                             \
    }                                                      \
} while (0)

#define DBG_PROG(...)                                   \
do {                                                    \
    if (get_dbg_level () >= DBG_LEVEL_PROG) {           \
        PRINT(__VA_ARGS__);                             \
        fflush(stdout);                             \
    }                                                   \
} while (0)

#define DBG_ERR(fmt, ...)                               \
do {                                                    \
    if (get_dbg_level () >= DBG_LEVEL_ERR) {            \
        PRINT("ERROR: [%s][Line:%d]" fmt"\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);            \
        fflush(stdout);                             \
    }                                                   \
} while (0)

#define err_sys(x)         { perror(x); return(1); }

#endif 